package ign.behaviours.ihm;

import ign.Performatives;
import ign.agents.IHM;
import jade.core.AID;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import java.awt.Rectangle;

/**
 * Comportement trian la boîte aux lettre pour l'ihm.
 * @author Simon Bourigaut & Ghislain Di Paolo
 */
public class Listening extends Behaviour {
	private static final long serialVersionUID = 1101529304059657649L;

	@Override
	public void action() {
		ACLMessage msg = myAgent.receive();

		if (msg == null) 
			return ;

		int perf = msg.getPerformative();
		if (perf == Performatives.NEW_POSITION){

			((IHM)myAgent).resumeChecking() ;

			String content = msg.getContent();
			String[] params = content.split(";");

			int x = Integer.parseInt(params[0]);
			int y = Integer.parseInt(params[1]);
			int width = Integer.parseInt(params[2]);
			int height = Integer.parseInt(params[3]);

			Rectangle r = new Rectangle(x, y, width, height);
			AID building = msg.getSender();

			IHM ihm = (IHM) myAgent;
			ihm.putPosition(building, r);

		} else if (perf == Performatives.NEW_BUILDING){

			((IHM)myAgent).resumeChecking() ;

			String content = msg.getContent();
			String[] params = content.split(";");

			int x = Integer.parseInt(params[0]);
			int y = Integer.parseInt(params[1]);
			int width = Integer.parseInt(params[2]);
			int height = Integer.parseInt(params[3]);

			Rectangle r = new Rectangle(x, y, width, height);
			AID building = msg.getSender();

			IHM ihm = (IHM) myAgent;
			ihm.addBuilding(building, r);

		} 

		else if(perf == Performatives.GOODBYE_WORLD) {		
			((IHM)myAgent).remove(msg.getSender());
		}
	}

	@Override
	public boolean done() {
		
		return false;
	}
}
